package com.aivin.lib_japaneselearn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.lib_base.tool.FileUtil;
import com.aivin.lib_base.tool.RouteUtils;
import com.aivin.lib_media.myMp3Player.WordReadTool;
import com.aivin.lib_widget.bean.MarginDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import java.util.List;

@Route(path = RouteUtils.JapneseShowActivity)
public class JapneseShowActivity extends Activity implements View.OnClickListener {
    private ImageView pingWordGif;
    private ImageView pianWordGif;
    private TextView luomaShowTv;
    private WordReadTool wordReadTool = new WordReadTool();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.janpaneselearning_layout);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.color_333242)
                .init();

        initView();
        FileUtil fileUtil = new FileUtil(getApplicationContext(), handler);
        fileUtil.copyAssetsToSD("japan", FileUtil.getWkReaderRoot());
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            /*if (msg.what == FileUtil.SUCCESS) {
                WkLog.showLog("文件复制成功");
            }
            if (msg.what == FileUtil.FAILED) {
                WkLog.showLog("文件复制失败="+ msg.obj.toString());
            }*/
        }
    };

    private void initView() {
        pingWordGif = findViewById(R.id.pingWordGif);
        pianWordGif = findViewById(R.id.pianWordGif);
        luomaShowTv = findViewById(R.id.luomaShowTv);

        RecyclerView recyclerView = findViewById(R.id.wordShowRv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        recyclerView.addItemDecoration(new MarginDecoration(this));

        List<String[]> allWord = JapanWordConfig.getAllWords();
        String footerInfo = getString(R.string.app_jpaninfo);
        recyclerView.setAdapter(new JapanWordAdapter(allWord, footerInfo,
                new JapanWordAdapter.JapanwordSelectCallback() {
                    @Override
                    public void onSelect(String[] info) {
                        if (info == null) {
                            return;
                        }

                        String gifPing = FileUtil.getWkReaderRoot() + "/ping_pic/" + info[0] + ".gif";
                        ;
                        String gifPian = FileUtil.getWkReaderRoot() + "/pian_pic/" + info[1] + ".gif";

                        showGif(gifPing, pingWordGif);
                        showGif(gifPian, pianWordGif);

                        // 平假和片假读音是一样的
                        String mp3Src = FileUtil.getWkReaderRoot() + "/ping_voice/" + info[0] + ".mp3";
                        ;
                        if (wordReadTool != null) {
                            wordReadTool.readThWord(mp3Src);
                        }

                        luomaShowTv.setText(info[2]);
                    }
                }));

        // add  footer
        View footer = LayoutInflater.from(this).inflate(R.layout.app_recyclerview_footer_lay, recyclerView, false);
        JapanWordAdapter adapter = (JapanWordAdapter) recyclerView.getAdapter();
        if (adapter != null) {
            adapter.setFooterView(footer);
        }

    }


    @Override
    public void onClick(View v) {

    }

    private void showGif(String gifSrc, ImageView imageView) {
        imageView.setVisibility(View.VISIBLE);
        Glide.with(getApplicationContext())
                .load(gifSrc)
                .into(imageView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wordReadTool != null) {
            wordReadTool.release();
        }
    }


    private void test() throws  Exception {
        Class targetClass = Class.forName("com.xxx.xxx.xxxActivity") ;
        Intent intent = new Intent(this , targetClass) ;
        startActivity(intent);
    }
}
